package sanphone.com.runningschool.MyMessage;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.List;

import sanphone.com.runningschool.R;
import sanphone.com.runningschool.Saying.SayingDetailActivity;
import sanphone.com.runningschool.entity.Question;

public class MyMessageAdapter extends BaseAdapter {
    private Context mContext;
    private List<Question> questionList;

    public MyMessageAdapter(Context mContext, List<Question> questionList) {
        this.mContext = mContext;
        this.questionList = questionList;
    }

    @Override
    public int getCount() {
        return questionList.size();
    }

    @Override
    public Object getItem(int position) {
        return questionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        LayoutInflater inflater= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(convertView==null){
            holder=new ViewHolder();
            convertView=inflater.inflate(R.layout.message_item,null);
            holder.messageContent=convertView.findViewById(R.id.message_content);
            holder.detailText=convertView.findViewById(R.id.message_detail);
            holder.messageTitle=convertView.findViewById(R.id.message_title);
            holder.dayText=convertView.findViewById(R.id.day);
            holder.monthText=convertView.findViewById(R.id.month);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        final Question q=questionList.get(position);
        String content=null,title=null;
        try {
           content= URLDecoder.decode(q.getQuestionText(),"UTF-8");
           title= URLDecoder.decode(q.getQuestionTitle(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        holder.messageContent.setText(content);
        holder.messageTitle.setText(title);
        holder.monthText.setText(q.getCreatedTime().substring(5,7)+"月");
        holder.dayText.setText(q.getCreatedTime().substring(8,10));
        Log.e("date ",q.getCreatedTime() );
        final String finalContent = content;
        final String finalTitle = title;
        holder.detailText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,SayingDetailActivity.class);
                intent.putExtra("userName",q.getNickName());
                intent.putExtra("context", finalContent);
                intent.putExtra("questionId",q.getQuestionId());
                intent.putExtra("questionTitle", finalTitle);
                view.getContext().startActivity(intent);

            }
        });
        holder.messageContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,SayingDetailActivity.class);
                intent.putExtra("userName",q.getNickName());
                intent.putExtra("context", finalContent);
                intent.putExtra("questionId",q.getQuestionId());
                intent.putExtra("questionTitle",finalTitle);
                v.getContext().startActivity(intent);

            }
        });
        return convertView;
    }
    class ViewHolder{
        TextView messageContent,detailText,messageTitle,dayText,monthText;

    }
}
